package net.maku.module.util;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;

import net.maku.module.dao.AgCategoryDao;
import net.maku.module.entity.AgCategoryEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.List;


@Slf4j
@Configuration
@EnableScheduling
public class ProjectSchedule {


    @Autowired
    private AgCategoryDao agCategoryDao;

    /**
     * 5秒执行一次
     */
    @Scheduled(fixedRate = 5000)
    private void orderTasks() {

        //处理定时发布
        QueryWrapper<AgCategoryEntity> queryWrapper = new QueryWrapper<AgCategoryEntity>();
        queryWrapper.eq("status", 0);
        queryWrapper.isNotNull("publish_time");

        List<AgCategoryEntity> agCategoryEntities = agCategoryDao.selectList(queryWrapper);
        if (!CollectionUtils.isEmpty(agCategoryEntities)) {
            agCategoryEntities.forEach(e -> {
                if (!StringUtils.isEmpty(e.getPublishTime())) {
                    String publishTime = e.getPublishTime();
                    if (checkTime(publishTime)) {
                        e.setStatus(1);
                        //e.setPublishTime("");
                        agCategoryDao.updateById(e);
                    }

                }
            });
        }
    }

    @Scheduled(fixedRate = 5000)
    private void orderTasks1() {

        //处理定时发布
        QueryWrapper<AgCategoryEntity> queryWrapper = new QueryWrapper<AgCategoryEntity>();
        queryWrapper.eq("status", 1);
        queryWrapper.isNotNull("revoke_time");

        List<AgCategoryEntity> agCategoryEntities = agCategoryDao.selectList(queryWrapper);
        if (!CollectionUtils.isEmpty(agCategoryEntities)) {
            agCategoryEntities.forEach(e -> {
                if (!StringUtils.isEmpty(e.getRevokeTime())) {
                    String publishTime = e.getRevokeTime();
                    if (checkTime(publishTime)) {
                        e.setStatus(0);
                        //e.setRevokeTime("");
                        agCategoryDao.updateById(e);
                    }

                }
            });
        }
    }

//    public static Boolean checkTime(String targetTimeStr) {
//        // 目标时间
//        //String targetTimeStr = "2025-08-29 17:00:00";
//        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//        LocalDateTime targetTime = LocalDateTime.parse(targetTimeStr, formatter);
//
//        // 当前时间
//        LocalDateTime currentTime = LocalDateTime.now();
//
//        // 判断时间先后
//        if (currentTime.isBefore(targetTime)) {
//            return false;
//        }
//        return true;
//
//    }

    public static Boolean checkTime(String targetTimeStr) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime targetTime = LocalDateTime.parse(targetTimeStr, formatter);

        // 当前时间（精确到分钟）
        LocalDateTime currentTime = LocalDateTime.now().truncatedTo(ChronoUnit.MINUTES);

        // 目标时间（精确到分钟）
        targetTime = targetTime.truncatedTo(ChronoUnit.MINUTES);

        return currentTime.equals(targetTime);
    }

    public static void main(String[] args) {
        System.out.println(checkTime("2025-09-18 10:26:09"));
    }

}
